package automatica.autoprove;

import com.maplesoft.externalcall.MapleException;
import com.maplesoft.openmaple.EngineCallBacksDefault;

public class CallBacks extends EngineCallBacksDefault 

{ 

    boolean limitExceeded; 
    private StringBuffer output = new StringBuffer(100);



	public CallBacks() 

    { 

        limitExceeded = false; 

    } 

    public void textCallBack( Object data, int tag, String output ) 

            throws MapleException 

    { 

        switch ( tag ) 

        { 

            case MAPLE_TEXT_OUTPUT: 

                //System.out.print( "Text: " ); 

                break; 

            case MAPLE_TEXT_DIAG: 

                System.out.print( "Diag: " ); 

                break; 

            case MAPLE_TEXT_MISC: 

                System.out.print( "Misc: " ); 

                break; 

            case MAPLE_TEXT_HELP: 

                System.out.print( "Help: " ); 

                break; 

            case MAPLE_TEXT_QUIT: 

                System.out.print( "Quit: " ); 

                break; 

            case MAPLE_TEXT_WARNING: 

                System.out.print( "Warning: " ); 

                break; 

            case MAPLE_TEXT_DEBUG: 

                System.out.print( "Debug: " ); 

                break; 

        } 

        System.out.println( output );
    
        this.output.append("<span class='text'>"+output+"</span><br/>");

    } 

    public void errorCallBack( Object data, int offset, String msg ) 

            throws MapleException 

    { 

        if ( offset >= 0 ) 

        { 

            throw new MapleException( "Error: "+msg ); 

        } 

        else 

        { 

            throw new MapleException( "syntax error at offset "+offset+ 

                    ": "+msg ); 

        } 

    } 

    public void statusCallBack( Object data, long kbused, long kballoced, 

            double timeused ) 

                throws MapleException 

    { 

        if ( timeused > 100 ) 

        { 

            limitExceeded = true; 

        } 

        System.out.println( "Status: used = "+kbused+", alloced = "+ 

            kballoced+", time = "+timeused );
        
        this.output.append("<span class='status'><small>Status: used = "+kbused+", alloced = "+ 

            kballoced+", time = "+timeused + "</small></span><br/>");

    } 

    public String readLineCallBack( Object data, boolean debug ) 

            throws MapleException 

    { 

        return "readline"; 

    } 

    public boolean redirectCallBack( Object data, String output, boolean 

append ) 

            throws MapleException 

    { 

        System.out.println( "redirect to "+output+" append = "+append ); 

        return true; 

    } 

    public String callBackCallBack( Object data, String output ) 

            throws MapleException 

    { 

        System.out.println( "callback: "+output ); 

        return output+";"; 

    } 

    public boolean queryInterrupt( Object data ) 

            throws MapleException 

    { 

        return limitExceeded; 

    } 

    public String streamCallBack( Object data, String name, String args[] ) 

            throws MapleException 

    { 

        StringBuffer sbuf; 

        int i; 

        System.out.print( name+"( " ); 

        for ( i = 0; i < args.length; i++ ) 

        { 

            System.out.print( args[i]+" " ); 

        } 

        System.out.println( ")" ); 

        return args[0]+":"; 

    } 


    public StringBuffer getOutput() {
		return output;
	}

	public void setOutput(StringBuffer output) {
		this.output = output;
	}
}